import traceback

from spider.ershou_spider import ErShouSpider
from store.HouseStore import HouseStore
from store.XiaoquStore import XiaoquStore
from spider.base_spider import BaseSpider
from baseclass.send_email import SendMail
from lib.utility.log import *

class CrawlHouse:

    def __init__(self,channel="lianjia"):
        self.spider = ErShouSpider(channel,min_price=200,max_price=650)
        self.xiaoqu_store = XiaoquStore()
        self.house_store = HouseStore()
        self.mail = SendMail()

    def crawl_all(self):
        self.spider.start_crawl()

    def crawl_house_by_xiaoqu(self,district=None,area=None,id_list=[],name=None,min_price=3,max_price=15):
        try:
            xiao_info_list = self.xiaoqu_store.query(district, area, id_list, name, min_price, max_price)
        except Exception as e:
            logger.error("查询小区列表出错.{0}".format(e),exc_info=traceback.print_exc())
        if not xiao_info_list or xiao_info_list is None:
            return
        for xiaoqu in xiao_info_list:
            BaseSpider.random_delay()
            logger.info("开始爬取小区的房子：{0}.{1}.{2}".format(xiaoqu["district"], xiaoqu["xuequ_id"], xiaoqu["xiaoqu_id"]))
            try:
                self.spider.crawl_house_list(xiaoqu["district"], xiaoqu["xuequ_id"], xiaoqu["xiaoqu_id"])
                logger.info("已经成功爬取小区的房子：{0}.{1}.{2}".format(xiaoqu["district"], xiaoqu["xuequ_id"],
                                                                    xiaoqu["xiaoqu_id"]))
            except Exception as e:
                logger.error("爬取小区{0}。出错。e:{1}".format(xiaoqu["xiaoqu_id"],traceback.print_exc()),exc_info=traceback.print_exc())
        logger.info("全部小区爬取完毕。共"+str(len(xiao_info_list))+"个")

    def crawl_house_detail(self,district, area, xiaoqu_id,house_id):
        if area is None or xiaoqu_id is None:
            house_info = self.house_store.get_by_house_id(house_id)
            area,xiaoqu_id = house_info["xuequ_id"], house_info["xiaoqu_id"]
        try:
            return self.spider.crawl_house_detail(district, area, xiaoqu_id, house_id)
        except Exception as e:
            #可能下架了，直接操作下架
            self.house_store.set_status(house_id,1)
            self.mail.send_email(subject="房源详情页出现异常", msghtml="house_id:{0}.异常:{1}.已经操作下架".format(house_id, traceback.print_exc()))
            logger.error("house_id:{0}.异常:{1}.已经操作下架".format(house_id, e),exc_info=traceback.print_exc())
            # raise e


if __name__ == "__main__":
    crawler = CrawlHouse()
    crawler.crawl_house_by_xiaoqu(id_list=[""],min_price=4,max_price=10)
